# encoding=utf8
import sys
import math
reload(sys)
sys.setdefaultencoding('utf8')

class JT_0802:
    result = []
    analysisDescription = ["应答流水号:", "多媒体数据总项数:", "多媒体ID:", "多媒体类型:","通道ID：", "事件项编码:","位置信息汇报（0x0200）消息体："]
    analysis0802Length = [2,2,4,1,1,1,28]
    WarnInfo = ["非法开门报警", "侧翻预警", "碰撞预警", "车辆非法位移", "车辆非法点火", "车辆被盗(通过车辆防盗器)", "车辆油量异常", "车辆VSS故障",
                "路线偏离报警", "路段行驶时间不足/过长", "进出路线", "进出区域", "超时停车", "当天累计驾驶超时", "保留", "保留", "保留", "疲劳驾驶预警",
                "超速预警", "道路运输证IC卡模块故障", "摄像头故障", "TTS模块故障", "终端LCD或显示器故障", "终端主电源掉电", "终端主电源欠压", "GNSS天线短路",
                "GNSS天线未接或被剪断", "GNSS模块发生故障", "危险预警", "疲劳驾驶", "超速报警", "紧急报警"]
    StatusInfo = [["保留", "保留"], ["保留", "保留"], ["保留", "保留"], ["保留", "保留"], ["保留", "保留"], ["保留", "保留"], ["保留", "保留"],
                  ["保留", "保留"], ["保留", "保留"], ["保留", "保留"], ["未使用Galileo卫星进行定位", "使用Galileo卫星进行定位"],
                  ["未使用GLONASS卫星进行定位", "使用GLONASS卫星进行定位"], ["未使用北斗卫星进行定位", "使用北斗卫星进行定位"],
                  ["未使用GPS卫星进行定位", "使用GPS卫星进行定位"], ["门5关", "门5开"], ["门4关", "门4开"], ["门3关", "门3开"], ["门2关", "门2开"],
                  ["门1关", "门1开"], ["车门解锁", "车门加锁"], ["车辆电路正常", "车辆电路断开"], ["车辆油路正常", "车辆油路断开"],
                  ["空车", "半载", "保留", "满载"], ["空车", "半载", "保留", "满载"], ["保留", "保留"], ["保留", "保留"],
                  ["经纬度未经保密插件加密", "经纬度已经保密插件加密"], ["运营状态", "停运状态"], ["东经", "西经"], ["北纬", "南纬"], ["未定位", "已定位"],
                  ["ACC关", "ACC开"]]
    WarnStrInfo = ""
    StatusStrInfo = ""
    LatStrInfo = ""
    LongStrInfo = ""
    HeightStrInfo = ""
    SpeedStrInfo = ""
    DireStrInfo = ""
    TimeStrInfo = ""

    def LogicAisle(self,protocolStr,header):
        count = 0
        s =""
        for i in range(len(self.analysisDescription)):
            if (i == 0 or i == 1 or i == 2 or i==4):
                s = protocolStr[count:count + (self.analysis0802Length[i] * 2)]
                header.append(self.analysisDescription[i] + s + "\n");
                count = count + self.analysis0802Length[i] * 2;
            elif i== 3 :
                s = protocolStr[count:count + (self.analysis0802Length[i] * 2)]
                if s=="00" :
                    header.append(self.analysisDescription[i] + s + "图像" + "\n")
                    count = count + self.analysis0802Length[i] * 2;
                elif s == "01":
                    header.append(self.analysisDescription[i] + s + "音频" + "\n")
                    count = count + self.analysis0802Length[i] * 2;
                elif s == "02":
                    header.append(self.analysisDescription[i] + s + "视频" + "\n")
                    count = count + self.analysis0802Length[i] * 2;
            elif i== 5 :
                s = protocolStr[count:count + (self.analysis0802Length[i] * 2)]
                if s=="00" :
                    header.append(self.analysisDescription[i] + s + "平台下发指令" + "\n")
                    count = count + self.analysis0802Length[i] * 2;
                elif s == "01":
                    header.append(self.analysisDescription[i] + s + "定时动作" + "\n")
                    count = count + self.analysis0802Length[i] * 2;
                elif s == "02":
                    header.append(self.analysisDescription[i] + s + "抢劫报警触发" + "\n")
                    count = count + self.analysis0802Length[i] * 2;
                elif s == "03":
                    header.append(self.analysisDescription[i] + s + "碰撞侧翻报警触发" + "\n")
                    count = count + self.analysis0802Length[i] * 2;
            elif i==6:
                s = protocolStr[count:count + (self.analysis0802Length[i] * 2)]
                header.append(self.analysisDescription[i] + s + "\n");
                an = ['报警标志:', '状态:', '纬度:', '经度:', '高程:', '速度:', '方向:', '时间:']
                al = [4, 4, 4, 4, 2, 2, 2, 6]
                c = 0
                st=""
                for l in range(8):
                    st = s[c:c + (al[l] * 2)]
                    print(st)
                    if (l == 0):
                        st = self.hexstrtobin(al[l], st)
                        self.AnalysisWarnFlagBits(st)
                        header.append(an[l] + self.WarnStrInfo + "\n")
                    elif (l == 1):
                        st = self.hexstrtobin(al[l], st)
                        self.AnalysisStatusFlagBits(st)
                        header.append(an[l] + self.StatusStrInfo + "\n")
                    elif (l == 2):
                        self.AnalysisLat(st)
                        print(self.LatStrInfo)
                        header.append(an[l] + self.LatStrInfo + "\n")
                    elif (l == 3):
                        self.AnalysisLong(st)
                        header.append(an[l] + self.LongStrInfo + "\n")
                    elif (l == 4):
                        self.AnalysisHeight(st)
                        header.append(an[l] + self.HeightStrInfo + "\n")
                    elif (l == 5):
                        self.AnalysisSpeed(st)
                        header.append(an[l] + self.SpeedStrInfo + "\n")
                    elif (l == 6):
                        self.AnalysisDire(st)
                        header.append(an[l] + self.DireStrInfo + "\n")
                    elif (l == 7):
                        self.AnalysisTime(st)
                        header.append(an[l] + self.TimeStrInfo + "\n")
                    c = c + al[l] * 2
                count = count + 28 * 2
        self.result = header

    def hexstrtobin(self, len, str):
                # 转换为二进制字符串
                s = bin(int(str, 16))
                # 位数不足补零
                n = int(s[2:])
                n = "%032d" % n
                print
                "hexstrtobin.n=%s" % n
                print
                "hexstrtobin.n.type=%s" % type(n)
                # s=str(n)
                # print "hexstrtobin.s.type=%s"%type(s)
                return n

    def AnalysisStatusFlagBits(self, source):
                # char[] chars = source.toCharArray();
                # chars = array.array( 'u', source );
                chars = list(source)
                bit8_9 = "";
                print("AnalysisStatusFlagBits.source=%s\n" % source);
                print("AnalysisStatusFlagBits.chars=%s\n" % len(chars));
                for i in reversed(range(len(chars))):
                    print("AnalysisStatusFlagBits.i=%s\n" % i);
                    print("AnalysisStatusFlagBits.chars=%s\n" % chars[i]);
                    if ((len(chars) - i) == 9 or (len(chars) - i) == 10):
                        print("AnalysisStatusFlagBits.bit8_9=%s\n" % bit8_9);
                        # bit8_9 += String.valueOf(chars[i]);
                        bit8_9 += str(chars[i]);
                        print("AnalysisStatusFlagBits.bit8_9=%s\n" % bit8_9);
                        if ((len(chars) - i) == 10):
                            print("hkkkkkkkkkkkkkkkAnalysisStatusFlagBits.bit8_9=%s\n" % bit8_9);
                            if (bit8_9 == "00"):
                                print("hhhhhhhhhhAnalysisStatusFlagBits.bit8_9=%s\n" % bit8_9);
                                self.StatusInfoAnaly(i, 0);
                            elif (bit8_9 == "01"):
                                self.StatusInfoAnaly(i, 1);
                            elif (bit8_9 == "10"):
                                self.StatusInfoAnaly(i, 2);
                            elif (bit8_9 == "11"):
                                self.StatusInfoAnaly(i, 3);
                        continue;

                    # self.StatusInfoAnaly(i, Character.getNumericValue(chars[i]));
                    # self.StatusInfoAnaly(i, int(chars[i],16));
                    self.StatusInfoAnaly(i, int(chars[i]));

    def WarnInfoAnaly(self, num):
                if (self.WarnInfo[num] != "保留"):
                    self.WarnStrInfo += self.WarnInfo[num];
                    self.WarnStrInfo += "; \n";

    def AnalysisWarnFlagBits(self, source):
                print("AnalysisWarnFlagBits.source=%s" % source);
                print
                type(source)
                # chars = array.array( 'u', source );
                # char[] chars = source.toCharArray();
                chars = list(source)
                print
                type(chars)
                print("AnalysisWarnFlagBits.chars=%s" % chars[2]);
                print("AnalysisWarnFlagBits.chars=%d" % len(chars));
                k = True;
                for i in range(len(chars)):
                    print("AnalysisWarnFlagBits.chars=%s" % chars[i]);
                    if (chars[i] == '1'):
                        self.WarnInfoAnaly(i);
                        k = False;

    def StatusInfoAnaly(self, i, j):
                print("StatusInfoAnaly.i=%s" % i);
                print("StatusInfoAnaly.j=%s" % j);
                if (self.StatusInfo[i][j] != "保留"):
                    self.StatusStrInfo += self.StatusInfo[i][j];
                    self.StatusStrInfo += "; \n";

    def AnalysisLat(self, source):
                # double Lat;
                try:
                    Lat = int(source, 16) * math.pow(10, -6);
                    # self.LatStrInfo = String.format("%.6f", Lat);
                    self.LatStrInfo = "%.6f" % Lat
                except:
                    pass

    def AnalysisLong(self, source):
                # double Long;
                try:
                    Long = int(source, 16) * math.pow(10, -6);
                    # self.LongStrInfo = String.format("%.6f", Long);
                    self.LongStrInfo = "%.6f" % Long
                except:
                    pass

    def AnalysisHeight(self, source):
                # int height;
                try:
                    height = int(source, 16);
                    self.HeightStrInfo = str(height);
                except:
                    pass

    def AnalysisSpeed(self, source):
                # double speed;
                try:
                    speed = int(source, 16) * math.pow(10, -1);
                    # SpeedStrInfo = String.format("%.2f", speed);
                    self.SpeedStrInfo = "%.1f" % speed
                except:
                    pass

    def AnalysisDire(self, source):
                # int Dire;
                try:
                    Dire = int(source, 16);
                    # DireStrInfo = String.valueOf(Dire);
                    self.DireStrInfo = str(Dire);
                except:
                    pass

    def AnalysisTime(self, source):
                # StringBuilder sb = new StringBuilder (source);
                # sb.insert(0, "20");
                # sb.insert(4, "年");
                # sb.insert(7, "月");
                # sb.insert(10, "日");
                # sb.insert(13, "时");
                # sb.insert(16, "分");
                # sb.insert(19, "秒");
                # TimeStrInfo = sb.toString();
                timelist = list(source)
                timelist.insert(0, "20")
                timelist.insert(3, "年")
                timelist.insert(6, "月")
                timelist.insert(9, "日")
                timelist.insert(12, "时")
                timelist.insert(15, "分")
                timelist.insert(18, "秒")
                self.TimeStrInfo = "".join(timelist)



                
